package com.fosung.frame.modules.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.modules.sys.utils.HttpURLConnectionUtil;
import com.google.gson.Gson;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
@Transactional(readOnly = true)
public class I85service {

	public static final String GET_ORG = Global.getConfig("getOrg");
	public static final String QUERY_ORG = Global.getConfig("queryOrg");
	public static final String QUERY_PERSON = Global.getConfig("queryPerson");
	public static final String queryUserInfoByHash = Global.getConfig("queryUserInfoByHash");
	public static final String queryPersonByOrg = Global.getConfig("queryPersonByOrg");

	/**
	 * 获取当前党组织树接口
	 * 
	 * @param orgId
	 *            组织id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getOrg(String orgId) {
		String jsonStr = HttpURLConnectionUtil.getJsonByInternet(GET_ORG + orgId);
		JSONObject jasonObject = JSONObject.fromObject(jsonStr);
		Map<String, Object> map = (Map<String, Object>) jasonObject;
		return map;
	}

	/**
	 * 获取党组织树接口
	 * 
	 * @param orgId
	 *            组织id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryOrg(String orgId) {
		Gson gson = new Gson();
		String jsonStr = HttpURLConnectionUtil.getJsonByInternet(QUERY_ORG + orgId);
		List<Map<String, Object>> list2 = (List<Map<String, Object>>) gson.fromJson(jsonStr, Object.class);
		return list2;
	}

	/**
	 * 获取组织下党员列表 组织ID orgId string 必填
	 * 
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryPerson(String orgId) {
		String jsonStr = HttpURLConnectionUtil.getJsonByInternet(QUERY_PERSON + orgId);
		JSONObject jasonObject = JSONObject.fromObject(jsonStr);
		Map<String, List<Map<String, Object>>> map = (Map<String, List<Map<String, Object>>>) jasonObject;
		List<Map<String, Object>> list2 = map.get("users");
		return list2;
	}

	/**
	 * 获取组织下党员列表 组织ID orgId string 必填按用户排序
	 * 
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryPersonByOrg(String orgId) {
		String jsonStr = HttpURLConnectionUtil.getJsonByInternet(queryPersonByOrg + orgId);
		JSONObject jasonObject = JSONObject.fromObject(jsonStr);
		Map<String, List<Map<String, Object>>> map = (Map<String, List<Map<String, Object>>>) jasonObject;
		List<Map<String, Object>> list2 = map.get("users");
		return list2;
	}

	/**
	 * 接口名称根据身份证号和姓名的hash获取党员基本信息
	 * 
	 * @param hash
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> queryUserInfoByHash(String hash) {
		JSONArray array = new JSONArray();
		array.add(hash);
		String jsonStr = HttpURLConnectionUtil.postDownloadJson(queryUserInfoByHash, array.toString());
		JSONArray jsArr = JSONArray.fromObject(jsonStr.toString());
		Map<String, Object> map = new HashMap<String, Object>();
		if (jsArr != null && jsArr.size() > 0) {
			map = (Map<String, Object>) jsArr.opt(0);
		}
		return map;
	}

}
